package com.css.fxfzaqbz.base.dict.repository.impl;

import com.css.fxfzaqbz.base.dict.repository.DictItemRepository;
import com.css.fxfzaqbz.base.dict.repository.DictItemRepository;
import com.css.fxfzaqbz.base.dict.repository.entity.DictItemEntity;
import com.css.fxfzaqbz.base.dict.viewobjects.DictEntityVo;
import com.css.fxfzaqbz.base.dict.viewobjects.DictItemQueryParams;
import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.util.PlatformObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class DictItemRepositoryImpl implements DictItemRepository {
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public Page<DictItemEntity> queryDictItems(DictItemQueryParams queryParams, int curPage, int pageSize) {
        StringBuilder sql = new StringBuilder("select * from sys_dict_item ");
        StringBuilder whereSql = new StringBuilder("where 1=1 ");
        if(!PlatformObjectUtils.isEmpty(queryParams.getDictId())){
            whereSql.append(" and dict_id = "+ queryParams.getDictId());
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getDictItemName())){
            whereSql.append(" and dict_item_name like %"+ queryParams.getDictItemName() +"%");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getDictItemCode())){
            whereSql.append(" and dict_item_code like %"+ queryParams.getDictItemCode() +"%");
        }
        StringBuilder orderSql = new StringBuilder(" order by sort");

        String sqlWhole = sql.append(whereSql).append(orderSql).toString();

        List<DictItemEntity> list = jdbcTemplate.query(sqlWhole, new BeanPropertyRowMapper<>(DictItemEntity.class));

        Pageable pageable = PageRequest.of(curPage - 1 ,pageSize);

        Page<DictItemEntity> dictItems = new PageImpl<DictItemEntity>(list, pageable,PlatformObjectUtils.isEmpty(list) ? 0 : list.size());
        return dictItems;
    }

    @Override
    public void saveDictItem(DictItemEntity itemEntity) {
        /*StringBuilder sql = new StringBuilder("insert into sys_dict_item values(?,?,?,?,?,?,?,?,?,?,?) ");
        Object[] objects = new Object[11];
        objects[0] = itemEntity.getDictId();
        objects[1] = itemEntity.getDictItemId();
        objects[2] = itemEntity.getDictItemCode();
        objects[3] = itemEntity.getDictItemName();
        objects[4] = itemEntity.getIsValid();
        objects[5] = itemEntity.getSort();
        objects[6] = itemEntity.getDescription();
        objects[7] = itemEntity.getCreateUser();
        objects[8] = itemEntity.getCreateTime();
        objects[9] = itemEntity.getUpdateUser();
        objects[10] = itemEntity.getUpdateTime();
        jdbcTemplate.update(sql.toString(), objects);*/
        StringBuilder sql = new StringBuilder();
        if (PlatformObjectUtils.isNotEmpty(itemEntity) && PlatformObjectUtils.isNotEmpty(itemEntity.getDictItemId()) && PlatformObjectUtils.isNotEmpty(findById(itemEntity.getDictItemId())) ){
            sql.append("update sys_dict_item set dict_item_code = :dictItemCode, dict_item_name = :dictItemName, dict_id = :dictId, sort = :sort\n" +
                    ", is_valid = :isValid, description = :description, update_user = :updateUser, update_time = :updateTime  where dict_item_id = :dictItemId");
        }else {
            sql.append("insert into sys_dict_item values( :dictItemId, :dictItemCode, :dictItemName, :dictId, :sort\n" +
                    ", :isValid, :description, :createUser, :createTime, :updateUser, :updateTime ) ");
        }

        SqlParameterSource source = new BeanPropertySqlParameterSource(itemEntity);
        namedParameterJdbcTemplate.update(sql.toString(), source);
    }

    @Override
    public String findDictCodeByDictItemId(String dictItemId) {
        StringBuilder sql = new StringBuilder("select d.dict_code from sys_dict_item di,sys_dict d where d.dict_id = di.dict_id and d.is_valid = '"+ YNEnum.Y+"' and di.is_valid = '"+ YNEnum.Y+"' and di.dict_item_id = '"+ dictItemId +"'");
        try {
            String dictCode = jdbcTemplate.queryForObject(sql.toString(), String.class);
            return dictCode;
        }catch (EmptyResultDataAccessException e){
            return null;
        }
    }

    @Override
    public DictItemEntity findById(String id) {
        StringBuilder sql = new StringBuilder("select * from sys_dict_item where dict_item_id = '"+ id +"'");
        try {
            DictItemEntity dictItemEntity = jdbcTemplate.queryForObject(sql.toString(), new BeanPropertyRowMapper<>(DictItemEntity.class));
            return dictItemEntity;
        }catch (EmptyResultDataAccessException e){
            return null;
        }
    }

    @Override
    public void delete(List<String> ids) {
        StringBuilder sql = new StringBuilder("delete from sys_dict_item where dict_item_id in (:ids)");
        Map idMap = new HashMap();
        idMap.put("ids", ids);
        namedParameterJdbcTemplate.update(sql.toString(), idMap);
    }

    @Override
    public DictItemEntity findOneByDictItemCodeAndDictId(String dictItemCode, String dictId) {
        StringBuilder sql = new StringBuilder("select * from sys_dict_item where dict_item_code = '"+ dictItemCode +"' and dict_id = '"+ dictId +"'");
        try {
            DictItemEntity dictItemEntity = jdbcTemplate.queryForObject(sql.toString(), new BeanPropertyRowMapper<>(DictItemEntity.class));
            return dictItemEntity;
        }catch (EmptyResultDataAccessException e){
            return null;
        }
    }

    @Override
    public List<DictItemEntity> findAllById(List<String> ids) {
        StringBuilder sql = new StringBuilder("select * from sys_dict_item where dict_item_id in (:ids)");
        Map idsMap = new HashMap();
        idsMap.put("ids", ids);
        List list = namedParameterJdbcTemplate.query(sql.toString(), idsMap, new BeanPropertyRowMapper<>(DictItemEntity.class));
        return list;
    }

    @Override
    public List<String> findDictCodeByDictItemIds(List<String> ids) {
        StringBuilder sql = new StringBuilder("select d.dict_code from sys_dict_item di,sys_dict d where d.dict_id = di.dict_id and d.is_valid = '"+ YNEnum.Y+"' and di.is_valid = '"+ YNEnum.Y+"' and di.dict_item_id IN (:ids)");
        Map idsMap = new HashMap();
        idsMap.put("ids", ids);
        List dictCodes = namedParameterJdbcTemplate.queryForList(sql.toString(), idsMap, String.class);
        return dictCodes;
    }

    @Override
    public DictItemEntity findDictItemByDictCodeAndDictItemCode(String dictCode, String dictItemCode) {
        StringBuilder sql = new StringBuilder("select di.* from sys_dict_item di,sys_dict d where d.dict_code = '"+ dictCode +"' and d.dict_id = di.dict_id and di.dict_item_code = '"+ dictItemCode +"'");
        try {
            DictItemEntity dictItemEntity = jdbcTemplate.queryForObject(sql.toString(), new BeanPropertyRowMapper<>(DictItemEntity.class));
            return dictItemEntity;
        }catch (EmptyResultDataAccessException e){
            return null;
        }
    }

    @Override
    public DictItemEntity findValidDictItemByDictCodeAndDictItemCode(String dictCode, String dictItemCode) {
        StringBuilder sql = new StringBuilder("select di.* from sys_dict_item di,sys_dict d where d.dict_code = '"+ dictCode +"' and d.dict_id = di.dict_id and di.dict_item_code = '"+ dictItemCode +"' and d.is_valid = '1' and di.is_valid = '1'");
        try {
            DictItemEntity dictItemEntity = jdbcTemplate.queryForObject(sql.toString(), new BeanPropertyRowMapper<>(DictItemEntity.class));
            return dictItemEntity;
        }catch (EmptyResultDataAccessException e){
            return null;
        }
    }

    @Override
    public DictItemEntity findValidDictItemByDictCodeAndDictItemName(String dictCode, String dictItemName) {
        StringBuilder sql = new StringBuilder("select di.* from sys_dict_item di,sys_dict d where d.dict_code = '"+ dictCode +"' and d.dict_id = di.dict_id and di.dict_item_name = '"+ dictItemName +"' and d.is_valid = '1' and di.is_valid = '1'");
        try {
            DictItemEntity dictItemEntity = jdbcTemplate.queryForObject(sql.toString(), new BeanPropertyRowMapper<>(DictItemEntity.class));
            return dictItemEntity;
        }catch (EmptyResultDataAccessException e){
            return null;
        }
    }

    @Override
    public List<DictItemEntity> findDictItemsByDictCode(String code) {
        StringBuilder sql = new StringBuilder("select di.* from sys_dict_item di,sys_dict d where d.dict_id = di.dict_id  and d.dict_code = '"+ code +"' order by di.sort");
        List<DictItemEntity> list = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(DictItemEntity.class));
        return list;
    }

    @Override
    public List<DictItemEntity> findValidDictItemsByDictCode(String code) {
        StringBuilder sql = new StringBuilder("select di.* from sys_dict_item di,sys_dict d where d.dict_id = di.dict_id and d.is_valid = '"+ YNEnum.Y+"' and di.is_valid = '"+ YNEnum.Y+"' and d.dict_code = '"+ code +"' order by di.sort ");
        List<DictItemEntity> list = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(DictItemEntity.class));
        return list;
    }

    @Override
    public void deleteByDictIds(List<String> dictIds) {
        StringBuilder sql = new StringBuilder("delete from sys_dict_item where dict_id in (:dictIds)");
        Map idMap = new HashMap();
        idMap.put("dictIds", dictIds);
        namedParameterJdbcTemplate.update(sql.toString(), idMap);
    }

    @Override
    public List<DictItemEntity> findDictItemByDictId(List<String> dictIds) {
        StringBuilder sql = new StringBuilder("select * from sys_dict_item where dict_id IN (:dictIds) and is_valid='1' order by sort");
        Map idMap = new HashMap();
        idMap.put("dictIds", dictIds);
        List list = namedParameterJdbcTemplate.query(sql.toString(), idMap, new BeanPropertyRowMapper<>(DictItemEntity.class));
        return list;
    }

    @Override
    public List<DictItemEntity> findAll() {
        StringBuilder sql = new StringBuilder("select * from sys_dict_item ");
        List<DictItemEntity> list = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(DictItemEntity.class));
        return list;
    }

    @Override
    public List<DictItemEntity> findDictItemByDictDirId(List<String> ids) {
        StringBuilder sql = new StringBuilder("select di.* from sys_dict_item di,sys_dict d ,sys_dict_directory dd  where dd.dict_dir_id = d.dict_dir_id  and d.dict_id = di.dict_id and  d.dict_dir_id IN (:ids)");
        Map idMap = new HashMap();
        idMap.put("ids", ids);
        List list = namedParameterJdbcTemplate.query(sql.toString(), idMap, new BeanPropertyRowMapper<>(DictItemEntity.class));
        return list;
    }

    @Override
    public DictItemEntity findOneByDictItemIdAndDictId(String dictId, String dictItemId) {
        StringBuilder sql = new StringBuilder("select * from sys_dict_item where dict_item_id = '"+ dictItemId +"' and dict_id = '"+ dictId +"'");
        try {
            return jdbcTemplate.queryForObject(sql.toString(), new BeanPropertyRowMapper<>(DictItemEntity.class));
        }catch (EmptyResultDataAccessException e){
            return null;
        }
    }

    @Override
    public List<DictItemEntity> findDictItemsByDictId(String dictId) {
        StringBuilder sql = new StringBuilder("select * from sys_dict_item where dict_id = :dictId and is_valid='1' order by sort");
        Map idMap = new HashMap();
        idMap.put("dictId", dictId);
        return namedParameterJdbcTemplate.query(sql.toString(), idMap, new BeanPropertyRowMapper<>(DictItemEntity.class));
    }

    @Override
    public List<DictEntityVo> getDictItemByDictDirId(String dictDirId) {
        String sql = "select dict.dict_id,dict_code,dict_name,item.dict_item_id,item.dict_item_code,item.dict_item_name from sys_dict dict,sys_dict_item item where dict.dict_id=item.dict_id and dict.is_valid='1' and item.is_valid='1' and dict_dir_id='"+dictDirId+"'";
        return jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(DictEntityVo.class));
    }
}

